package com.chance.cc.crawler.development.command.job.domain.xhs.applets;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequest;
import com.chance.cc.crawler.core.downloader.HttpRequestBody;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.queue.crawler.CrawlerQueueConfig;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.command.job.CrawlerJobCommand;
import com.chance.cc.crawler.development.scripts.xiaohongshu.applets.XHSAppletsAccountCrawlerScript;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;
import static com.chance.cc.crawler.development.command.publish.douyin.DYPublishCrawlerJob.metaServerIP;
import static com.chance.cc.crawler.development.command.publish.douyin.DYPublishCrawlerJob.metaServerPort;

/**
 * @author bx
 * @date 2020/12/6 0006 12:44
 */
public class XHSBDAccountNotesCrawlerScheduleJob {

    public static String domainId = "xhs";
    private static CrawlerJobCommand crawlerJobCommand = new CrawlerJobCommand(metaServerIP,metaServerPort);
    public static String articleQueue = StringUtils.joinWith("-","crawler",domainId,XHSAppletsAccountCrawlerScript.site,"queue");
    public static String articleIdsQueue = "account_article_ids";
    public static String accountNoteIdFilterQueue = "xhs_account_notes_filter";
    public static String crawlerArticleQueue = "crawler-xhs-xhs-bd-article-queue";


    public static void main(String[] args) {

        publishSearchCrawlerScheduleJobInfo();
        System.out.println(crawlerSchduler().generateCrawlerTriggerKey());
    }

    public static CrawlerJob publishSearchCrawlerScheduleJobInfo(){


        CrawlerJob crawlerSchduler =  crawlerSchduler();

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domainId);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerSchduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("小红书账户列表定时采集");
        crawlerScheduleJob.setCrawlerKey(crawlerSchduler.generateCrawlerKey());
        HttpPage page = crawlerJobCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布crawler作业：" + page.getRawText());
        return crawlerSchduler;

    }

    /**
     * dy采集
     * @return
     */
    public static CrawlerJob crawlerSchduler() {
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId, CrawlerEnum.CrawlerRequestType.turnPage)
                .httpUrl(XHSAppletsAccountCrawlerScript.startListUrl)
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
                .filter(CrawlerEnum.CrawlerRecordFilter.keyOrDateRange)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(accountNoteIdFilterQueue))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*7,null))
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.tagsCreator().bizTags().addDomain(domainId);
        requestRecord.tagsCreator().bizTags().addSite(XHSAppletsAccountCrawlerScript.site);

        CrawlerRequestRecord userKeyWordCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("xhs_user_note_kw", turnPageItem)
                .httpUrl("http://192.168.1.215:9599/v1/meta/xhs/keys?site=account_ids")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        CrawlerRequestRecord accountNotesInitCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("xhs_user_note_kw", turnPageItem)
                .httpUrl("http://192.168.1.215:9599/crawler/domain/common/api/v1/xhs/account/notes/init?" +
                        "accountNotesQueue="+articleIdsQueue+"&requestQueueName="+crawlerArticleQueue)
                .requestLabelTag(supportCallback)
                .requestLabelTag(internalDownload)
                .build();

        CrawlerRequestRecord articleRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest("xhs", CrawlerEnum.CrawlerRequestType.turnPage)
                .httpUrl(XHSAppletsAccountCrawlerScript.startListUrl)
                .releaseTime(System.currentTimeMillis())
                .build();
        requestRecord.tagsCreator().bizTags().addDomain("xhs");
        requestRecord.tagsCreator().bizTags().addSite(XHSAppletsAccountCrawlerScript.site);

        HttpRequest httpRequest = accountNotesInitCrawlerRecord.getHttpRequest();
        httpRequest.setMethod("POST");
        httpRequest.setRequestBody(HttpRequestBody.json(JSON.toJSONString(articleRequestRecord),"utf-8"));

        CrawlerQueueConfig articleIdsQueueConfig = new CrawlerQueueConfig(
                articleIdsQueue,
                CrawlerQueueConfig.Content.result,
                CrawlerQueueConfig.Storage.redis,
                CrawlerQueueConfig.Structure.list
        );



        return CrawlerJob.builder()
                .triggerInfo(
                        domainId,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-", domainId, XHSAppletsAccountCrawlerScript.site, CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerJobThreadNumber(1)
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(articleQueue))
                .queueResultPipeline(articleIdsQueue,articleIdsQueueConfig)
                .requestRecord(requestRecord)
                .supportRecord(userKeyWordCrawlerRecord)
                .supportRecord(accountNotesInitCrawlerRecord)
                .build();
    }

}
